package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.web.mvc.controller.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.Collection;
import java.util.Objects;

/**
 * User Manager Controller
 *
 * @author biwin
 */
@Path("/user")
public class UserController implements Controller {

    private static final String JSP_PREFIX = "/WEB-INF/jsp/common/";

    @Resource(name = "bean/UserService")
    private UserService userService;

    /**
     * create new account from request
     *
     * @param request
     * @param response
     * @return Forward register success page
     */
    @POST
    @Path("/doRegister")
    public String doRegister(HttpServletRequest request, HttpServletResponse response) {

        User user = builderUser(request);

        boolean register = false;
        try {
            register = userService.register(user);
        } catch (ConstraintViolationException e) {
            request.setAttribute("errors", e.getConstraintViolations());
            request.setAttribute("userName", user.getName());
        }
        if (register) {
            Collection<User> users = userService.getAll();

            HttpSession session = request.getSession();
            if (Objects.nonNull(users) && !users.isEmpty()) {
                request.setAttribute("users", users);
            }
            session.setAttribute("user", user);
            return JSP_PREFIX + "success.jsp";
        }
        return JSP_PREFIX + "failure.jsp";

    }

    /**
     * update user info
     *
     * @param request
     * @param response
     */
    @POST
    @Path("/doUpdate")
    public String doUpdateUser(HttpServletRequest request, HttpServletResponse response) {
        User user = builderUser(request);
        try {
            userService.update(user);
        } catch (ConstraintViolationException e) {
            request.setAttribute("errors", e.getConstraintViolations());
            request.setAttribute("userName", user.getName());
            return JSP_PREFIX + "failure.jsp";
        }
        Collection<User> all = userService.getAll();
        request.setAttribute("users", all);
        return JSP_PREFIX + "success.jsp";
    }

    private User builderUser(HttpServletRequest request) {
        User user = new User();
        if (Objects.nonNull(request.getParameter("id"))) {
            user.setId(Long.valueOf(request.getParameter("id")));
        }
        user.setName(request.getParameter("name"));
        user.setPassword(request.getParameter("password"));
        user.setEmail(request.getParameter("email"));
        user.setPhoneNumber(request.getParameter("phoneNumber"));
        return user;
    }


}
